'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
  let {projectID,userID} = event;
  let curUserData = {}; // 当前操作的用户
  
  let projectInDB = await db.collection('projects').doc(projectID).get();
  
  if(projectInDB.data.length !== 0 || projectInDB.affectedDocs !== 0){
  	  curUserData = projectInDB.data[0].partners.filter(item=>{
  		  return item.userID === userID
  	  })
  	  
  	  // 删除
  	  let removePartner = await db.collection('projects').doc(projectID).update({
  		  partners:dbCmd.pull({
  			userID
  		  })
  	  })
  	  
  	  // 重新追加
  	  curUserData[0].finishProjectDoc = {
		  fileName:"",
		  fileUrl:"",
		  time:Date.now()
		}
		
  	  let addPartner = await db.collection('projects').doc(projectID).update({
  		  partners:dbCmd.push(curUserData)
  	  })
  	  
  	  return {
  		  status:0,
  		  msg:"删除成功"
  	  }
  }
  return {
	  status:-1,
	  msg:"稍后重试"
  }
};
